<?php


namespace app\api\model;


use app\common\model\BaseModel;
use common;
use app\common\library\IDMan;
use think\Db;

const COUPON_STATE_NEW = 0;
const COUPON_STATE_PERFORMED = 100;
const COUPON_STATE_EXPIRED = 200;

//type 1 优惠卷 //type 2 折扣卷

class CouponModel extends BaseModel
{
    var $list = [
        [
            'type' => 1,
            'metadata' => [
                'bonus' => 200,
                'discount' => 1,
                'usage' => '首单减免',
            ],
        ],
    ];
    var $_info;

    protected $name = 'coupon';
    protected $pk = 'id';

    function create_coupon_type_1($user_id, $remark)
    {
        $ID = IDMan::order_id();
        $data = [
            'id' => $ID,
            'type' => 1,
            'passive' => 1,
            'user_id' => $user_id,
            'create_time' => date('Y-m-d H:i:s'),
            'metadata' => [
                'bonus' => 1000,
                'discount' => 1,
                'usage' => '优惠卷',
            ],
            'remark' => $remark,
            'name' => '优惠卷',
            'state' => 0,
            'expire_time' => date('Y-m-d H:i:s', strtotime("+1 month")),
        ];


        self::create($data,true);
        return $data;
    }

    function create_coupon_type_weekend($user_id,$remark)
    {
        $ID = IDMan::order_id();
        $data = [
            'id' => $ID,
            'type' => 1,
            'passive' => 1,
            'user_id' => $user_id,
            'create_time' => date('Y-m-d H:i:s'),
            'metadata' => [
                'bonus' => 100,
                'discount' => 1,
                'usage' => '优惠卷',
            ],
            'remark' => $remark,
            'name' => '优惠卷',
            'state' => 0,
            'expire_time' => date('Y-m-d H:i:s', strtotime("+25 hours")),
        ];


        self::create($data,true);
        return $data;
    }

    function create_coupon_oldusers($user_id)
    {
        $ID = IDMan::order_id();
        $data = [
            'id' => $ID,
            'type' => 1,
            'passive' => 1,
            'user_id' => $user_id,
            'create_time' => date('Y-m-d H:i:s'),
            'metadata' => [
                'bonus' => 100,
                'discount' => 1,
                'usage' => '优惠卷',
            ],
            'remark' => '老用户回馈',
            'name' => '优惠卷',
            'state' => 0,
            'expire_time' => date('Y-m-d H:i:s', strtotime("+1 week")),
        ];


        self::create($data,true);
        return $data;
    }

    function coupon_amount($info)
    {
//        if ($info && $info['type'] == 1) {
//            return $info['metadata']['bonus'];
//        }
        if ($info){
            return $info['metadata']['bonus'];
        }
        return 0;
    }

    function query_my_list($user_id, $cd, $pos, $limit)
    {
        $selectlist = [
            'id' => 'string',
            'type' => 'int',
            'user_id' => 'string',
            'create_time' => 'string',
            'passive' => 'int',
            'metadata' => 'array',
            'remark' => 'string',
            'name' => 'string',
            'state' => 'int',
            'expire_time' => 'string',
        ];

        $beans = [
            'user_id' =>$user_id,
            'state' => 0,
            'expire_time' => ['>=',date('Y-m-d H:i:s')],
        ];
        if (isset($cd['type'])) {
            $beans['type'] = $cd['type'];
        }
        if (isset($cd['passive'])) {
            $beans['passive'] = $cd['passive'];
        }

        $rows = $this->where($beans)->field(common::array_keys($selectlist))->limit($pos,$limit)->select();
        return $rows->toArray();
    }

    function query_list($user_id, $cd, $pos, $limit)
    {
        $selectlist = [
            'id' => 'string',                    //id
            'type' => 'int',                    //1：系统发放
            'user_id' => 'string',              //玩家id
            'create_time' => 'string',          //开始日期
            'passive' => 'int',
            'metadata' => 'array',              //优惠价价格数据
            'remark' => 'string',               //备注
            'name' => 'string',                 //名称
            'state' => 'int',                   //状态0表示未使用，100：已使用 101：已退还
            'expire_time' => 'string',          //结束时间
            'order_id'    => 'string',           //购买优惠券的订单
            'ride_order_id' => 'string',        //骑行订单
        ];

        $beans = [
            'user_id' =>$user_id,
        ];

        if (isset($cd['state'])) {
            $beans['state'] = $cd['state'];
        }


        if (isset($cd['start_time'])){
            $beans['create_time'] = ['>=',$cd['create_time']];
        }

        if (isset($cd['end_time'])){
            $beans['end_time'] = ['<',$cd['end_time']];
        }

        if (isset($cd['is_out_date'])){
            $is_out_date = $cd['is_out_date'];
            if ($is_out_date == 0){
                $beans['expire_time'] = ['>',date('Y-m-d H:i:s')];
            }
            else{
                $beans['expire_time'] = ['<',date('Y-m-d H:i:s')];
            }
        }



        if (isset($cd['type'])) {
            $beans['type'] = $cd['type'];
        }
        if (isset($cd['passive'])) {
            $beans['passive'] = $cd['passive'];
        }

        $rows = $this->where($beans)->field(common::array_keys($selectlist))->limit($pos,$limit)->select();
        return $rows->toArray();
    }


    function query_info($id)
    {
        $selectlist = [
            'id' => 'string',
            'type' => 'int',
            'passive' => 'int',
            'user_id' => 'string',
            'create_time' => 'string',
            'metadata' => 'array',
            'remark' => 'string',
            'name' => 'string',
            'state' => 'int',
            'expire_time' => 'string',
            'ride_order_id' => 'string',
        ];
        $beans = [
            'id'=>$id,
        ];

        $row = $this->where($beans)->field(common::array_keys($selectlist))->find();

        return $row;
    }

    function perform($id,$ride_order_id)
    {
        $data = [
            'state' => 100,
            'perform_time' => date('Y-m-d H:i:s'),
            'ride_order_id' => $ride_order_id,
        ];
        $where = [
            'id' => $id,
        ];

        self::update($data, $where);
    }

    function check($ids)
    {
        $aff =  Db::name($this->name)->where('id','in',$ids)->update(['state'=>0]);
        return $aff;
    }

    //赠送
    function coupon_for_gift_type_1($user_id,$amount,$remark)
    {
        $ID = IDMan::order_id();
        $data = [
            'id' => $ID,
            'type' => 1,
            'passive' => 1,
            'user_id' => $user_id,
            'create_time' => date('Y-m-d H:i:s'),
            'metadata' => [
                'bonus' => $amount,
                'discount' => 1,
                'usage' => '优惠卷',
            ],
            'remark' => $remark,
            'name' => '优惠卷',
            'state' => 0,
            'expire_time' => date('Y-m-d H:i:s', strtotime("+1 month")),
        ];


        self::create($data,true);
        return $data;
    }

    function create_coupon_May_Day($amount,$user_id,$remark)
    {
        $ID = IDMan::order_id();
        $data = [
            'id' => $ID,
            'type' => 1,
            'passive' => 1,
            'user_id' => $user_id,
            'create_time' => date('Y-m-d H:i:s'),
            'metadata' => [
                'bonus' => $amount,
                'discount' => 1,
                'usage' => '优惠券',
            ],
            'remark' => $remark,
            'name' => '优惠券',
            'state' => 0,
            'expire_time' => '2020-05-06 23:59:59',
        ];


        self::create($data,true);
        return $data;
    }

    //赠送
    function coupon_for_type_1($user_id,$amount,$duration,$remark)
    {
        $ID = IDMan::order_id();

        $start_time = date('Y-m-d H:i:s');
        $expire_time = date('Y-m-d H:i:s', strtotime($start_time)+$duration);
        $data = [
            'id' => $ID,
            'type' => 1,
            'passive' => 1,
            'user_id' => $user_id,
            'create_time' => $start_time,
            'metadata' => [
                'bonus' => $amount,
                'discount' => 1,
                'usage' => '优惠卷',
            ],
            'remark' => $remark,
            'name' => '优惠卷',
            'state' => 0,
            'expire_time' => $expire_time,
        ];


        self::create($data,true);
        return $data;
    }

    //折扣卷
    function coupon_for_type_2($user_id,$dis,$duration,$remark)
    {
        $ID = IDMan::order_id();

        $start_time = date('Y-m-d H:i:s');
        $expire_time = date('Y-m-d H:i:s', strtotime($start_time)+$duration);
        $data = [
            'id' => $ID,
            'type' => 2,
            'passive' => 1,
            'user_id' => $user_id,
            'create_time' => $start_time,
            'metadata' => [
                'bonus' => $dis,
                'discount' => 1,
                'usage' => '折扣卷',
            ],
            'remark' => $remark,
            'name' => '折扣卷',
            'state' => 0,
            'expire_time' => $expire_time,
        ];


        self::create($data,true);
        return $data;
    }

}